$(function () {
    var parm=getParms("categories");
    if(parm=="")
        getCategories();
    else
        getArticleTitleByCategoriesName(parm);
})

function getCategories() {
    $.post({
        url: baseurl+api.categories,
        success: function (res) {
            if(res.code=="402")
            $("#total").html("目前共计0个分类");
            else{
                $("#total").html("目前共计"+res.data.count+"个分类");
                var s="";
                $.each(res.data.info,function (index, info) {
                    s+=" <li class=\"category-list-item\">\n" +
                        "                                        <a class=\"category-list-link\" href=\"/categories/"+info.name+"\">\n" +
                        "                                           "+info.name+"\n" +
                        "                                        </a>\n" +
                        "                                    <span class=\"category-list-count\">"+info.count+"</span>\n" +
                        "                                </li>";
                })
                $("#itercategories").html(s);
            }
        }
    })
}
function getArticleTitleByCategoriesName(categroiesName) {
    $.post({
        url: baseurl+api.categories+"/"+categroiesName,
        success: function (res) {
            if(res.code=="200"){
                var s="";
                $.each(res.data,function (index,info) {
                    s+="                <article class=\"post post-type-normal\" style=\"opacity: 1; display: block; transform: translateY(0px);\">\n" +
                        "                    <header class=\"post-header\">\n" +
                        "                        <h1 class=\"post-title\">\n" +
                        "                            <a class=\"post-title-link\" href='/detail/"+info.date+"/"+info.description+"'>"+info.title+"</a>\n" +
                        "                        </h1>\n" +
                        "                        <div class=\"post-meta\">\n" +
                        "                                <span class=\"post-time\">\n" +
                        "                                    <span class=\"post-meta-item-icon\">\n" +
                        "                                        <i class=\"fa fa-calendar-o\"></i>\n" +
                        "                                    </span>\n" +
                        "                                    <span class=\"post-meta-item-text\">发表于</span>\n" +
                        "                                    <time title=\"创建于\" datetime=\"\">"+info.time+"</time>\n" +
                        "                                </span>\n" +
                        "                            <span class=\"post-category\">\n" +
                        "                                    <span class=\"post-meta-divider\">|</span>\n" +
                        "                                    <span class=\"post-meta-item-icon\">\n" +
                        "                                        <i class=\"fa fa-folder-o\"></i>\n" +
                        "                                    </span>\n" +
                        "                                    <span class=\"post-meta-item-text\">分类于</span>\n" +
                        "                                    <span>\n" +
                        "                                        <a href=\"/categories/"+info.categoriesName+"\" rel=\"index\">\n" +
                        "                                            <span>"+info.categoriesName+"</span>\n" +
                        "                                        </a>\n" +
                        "                                    </span>\n" +
                        "                                </span>\n" +
                        "                            <span class=\"post-comments-count\">\n" +
                        "                                    <span class=\"post-meta-divider\">|</span>\n" +
                        "                                    <span class=\"post-meta-item-icon\">\n" +
                        "                                      <i class=\"fa fa-comment-o\"></i>\n" +
                        "                                    </span>\n" +
                        "                                    <a href=\"javascript:void(0)\">\n" +
                        "                                      <span class=\"post-comments-count disqus-comment-count\" data-disqus-identifier=\"\">"+info.commentsCount+" Comments</span>\n" +
                        "                                    </a>\n" +
                        "                                </span>\n" +
                        "                            <span id=\"\" class=\"leancloud_visitors\" data-flag-title=\"\">\n" +
                        "                                    <span class=\"post-meta-divider\">|</span>\n" +
                        "                                    <span class=\"post-meta-item-icon\">\n" +
                        "                                        <i class=\"fa fa-eye\"></i>\n" +
                        "                                    </span>\n" +
                        "                                    <span class=\"post-meta-item-text\">阅读次数</span>\n" +
                        "                                    <span class=\"leancloud-visitors-count\">"+info.readCount+"</span>\n" +
                        "                                </span>\n" +
                        "                        </div>\n" +
                        "                    </header>\n" +
                        "\n" +
                        "                        <div class=\"post-body\" itemprop=\"articleBody\" style=\"opacity: 1; display: block; transform: translateY(0px);\">\n" +
                        "\n" +                      marked(info.content)+
                        "                        </div>\n" +
                        "                    <!--noindex-->\n" +
                        "                    <div class=\"post-button text-center\">\n" +
                        "                        <a class=\"btn\" href='/detail/"+info.date+"/"+info.description+"' rel=\"contents\">\n" +
                        "                            阅读全文 »\n" +
                        "                        </a>\n" +
                        "                    </div>\n" +
                        "                    <!--/noindex-->\n" +
                        "                    <footer class=\"post-footer\">\n" +
                        "                        <div class=\"post-eof\"></div>\n" +
                        "                    </footer>\n" +
                        "                </article>";
                })
                $("#posts").html(s);
            }else
                window.location.href="/notfound";
        }
    })
}
function getParms(name) {
    var url= window.location.href;
    var index=url.indexOf(name);
    var s = url.substring(index+name.length+1);
    return s;
}